package com.rjwh.dingdong.client.activity.download.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.adapter.DowloadListAdapter;
import com.rjwh.dingdong.client.bean.localbean.DownloadInfo;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.download.DownloadManager;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.util.RxBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Subscriber;

/**
 * 项目名称：whrjwh-parent
 * 类描述：下载列表
 * 创建人：wby
 * 创建时间：2016/6/23 14:31
 * 修改人：wby
 * 修改时间：2016/6/23 14:31
 * 修改备注：
 *
 */
public class DownLoadListFragment extends BaseFragment implements AdapterView.OnItemLongClickListener {
    @BindView(R.id.listview)
    ListView listview;
    // TODO: Rename and change types and number of parameters
    private DowloadListAdapter adapter;
    private Observable<DownloadInfo> observable;

    public static DownLoadListFragment newInstance() {
        DownLoadListFragment fragment = new DownLoadListFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.comm_listview, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        regiest();
        initView();
    }

    public void regiest() {
        observable = RxBus.get().register(LocalConstant.RX__POST_DOWNLOAD_REFRESH_LIST, DownloadInfo.class);
        Subscriber subscriber = new Subscriber<DownloadInfo>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(DownloadInfo downloadInfo) {
                initData();
            }
        };
        addSubscription(observable, subscriber);
    }

    public void initView() {
        adapter = new DowloadListAdapter(getActivity(), listview);
        listview.setAdapter(adapter);
        listview.setOnItemLongClickListener(this);
        adapter.startObserver();
        initData();
    }

    private void initData() {
        List<DownloadInfo> list = DownloadManager.getInstance().getmDownloadInfoList();
        adapter.clear();
        adapter.appendToList(list);
    }

    @Override
    public void onDestroy() {
        RxBus.get().unregister(LocalConstant.RX__POST_DOWNLOAD_REFRESH_LIST, observable);
        adapter.stopObserver();
        super.onDestroy();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        showDelDialog(adapter.getList().get(i));
        return false;
    }

    public void showDelDialog(final DownloadInfo info) {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        dialog.setMessage(getString(R.string.sure_to_delete));
        dialog.setNegativeButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                DownloadManager.getInstance().removeTask(info.getGameId());
                adapter.getList().remove(info);
                adapter.notifyDataSetChanged();
            }
        });
        dialog.setPositiveButton(getString(R.string.no), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        dialog.show();
    }


}
